#! /usr/bin/python

import math

def gcd(a, b):
    if b == 0: return a
    return gcd(b, a%b)

def A(n):
    start = int(math.log(n)) + 1
    x, k = 1, 1
    while x != 0:
        x = (x*10 + 1)%n
	k += 1
    return k

def isPrime(n):
    if n == 2 or n ==3 or n==5 or n==7: return True
    if n == 1 or n%2 == 0 or n%3 == 0: return False
    for i in range(5, int(math.sqrt(n) + 1), 6):
	if n%i == 0 or n%(i+2) == 0: return False
    return True

count = 0
ss = 0
print A(561)
for n in range(91, 10**6, 2):
    if gcd(n, 10) == 1 and isPrime(n) == False and (n-1)%A(n) == 0:
	ss += n
	count += 1
	if count == 25:
	   print ss
	   break
       
